package com.film.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.film.dao.FilmDao;
import com.film.domain.Film;
import com.film.rowMapper.FIlmPlaseRowMapper;
import com.film.rowMapper.FilmRowMapper;
import com.film.rowMapper.FilmShowTimeRowMapper;
import com.film.util.DBUtil;

public class FilmDaoImpl implements FilmDao {
	
	private JdbcTemplate temp = new JdbcTemplate(DBUtil.getDataScorse());
	//该方法用来查询所有的电影
	@Override
	public List<Film> selectAllFilm(int isDelete) {
		String sql = "select * from film where is_delete = ? order by boxoffice desc";
		return temp.query(sql, new FilmRowMapper(),isDelete);
	}
	
	
	
	//该方法用来做热门电影的查询
	@Override
	public List<Film> selectHotFilm(int isDelete) {
		
		String sql = "select * from film where expect=0 and is_delete=? order by boxoffice desc";
		
		return temp.query(sql,new FilmRowMapper(),isDelete);
	}

	
	//该方法用来做查询上映的电影
	@Override
	public List<Film> selectShowFilm(int isDelete) {
		
		String sql = "select * from film where expect=1 and is_delete=? order by boxoffice desc";
		
		return temp.query(sql,new FilmRowMapper(),isDelete);
	}

	
	//该方法用来做电影详情
	@Override
	public Film selectOneFilm(int filmId,int isDelete) {
		String sql = "select * from film where film_id = ? and is_delete=?";
		List<Film> lists = temp.query(sql, new FilmRowMapper(),filmId,isDelete);
		if(lists!=null) {
			return lists.get(0);
		}else {
			return null;
		}
	}

	@Override
	public List<String> selectFilmPlace() {
		String sql = "select DISTINCT film_place from film where is_delete=0";
		return temp.query(sql, new FIlmPlaseRowMapper());
	}
	
	
	
	//根据即将上映电影的分页条件查询上架的电影
	@Override
	public List<Film> selectFilmLimitShow(int pageNum, int stripNum,int isDelete) {
		String sql = "select * from film where expect = 1 and is_delete=? limit ?,? ";
		return temp.query(sql, new FilmRowMapper(),isDelete,pageNum,stripNum);
	}
	//根据热门电影的分页条件查询上架的电影
	@Override
	public List<Film> selectFilmLimitHot(int pageNum, int stripNum,int isDelete) {
		String sql = "select * from film where expect = 0 and is_delete=? limit ?,?";
		return temp.query(sql, new FilmRowMapper(),isDelete,pageNum,stripNum);
	}
	//用来做电影的假删除
	@Override
	public int updateFilm(int filmId,int isDelete) {
		String sql = "update film set is_delete = ? where film_id = ?";
		return temp.update(sql,isDelete,filmId);
	}



	@Override
	public int updateFilmReviewScore(String reviewScore,int film_id) {
		String sql = "update film set review_score = ?  where film_id = ? and is_delete=0";
		return temp.update(sql,reviewScore,film_id);
	}


  //电影的添加
	@Override
	public int addFilm(Film film,int except) {
		Connection conn = null;
		PreparedStatement pre = null;
		ResultSet res = null;
		int id = 0;
		try {
			conn = DBUtil.getConnecion();
			String sql = "INSERT INTO film (film_id,film_name, film_foreignName, film_place, film_time, film_showTime,film_img, is_delete, boxoffice, expect, film_lunguage, film_introduce) VALUES (default,?,?,?,?,?,?,0,1.5,?,?,?);"; 
			pre = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			pre.setString(1, film.getFilmName());
			pre.setString(2, film.getFilmForeignName());
			pre.setString(3, film.getFilmPlace());
			pre.setString(4, film.getFilmTime());
			pre.setString(5, film.getFilmShowTime());
			pre.setString(6,film.getFilmImg());
			pre.setInt(7,except);
			pre.setString(8, film.getFilmIunguage());
			pre.setString(9, film.getFilmIntroduce());
			
			int result = pre.executeUpdate();
			res= pre.getGeneratedKeys();
			if(res.next()) {
				id = res.getInt(1);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			DBUtil.closeAll(res, pre, conn);
		}
		return id;
	}



	@Override
	public int updateFilmInfo(Film film) {
		String sql = "update film set film_name = ?,film_foreignName = ?,film_place = ?,film_time = ?,film_showTime = ?,film_lunguage = ?,film_introduce = ? where film_id = ?";
		return temp.update(sql,film.getFilmName(),film.getFilmForeignName(),film.getFilmPlace(),film.getFilmTime(),film.getFilmShowTime(),film.getFilmIunguage(),film.getFilmIntroduce(),film.getFilmId());
	}



	@Override
	public int groundingFilm(int filmId) {
		String sql = "update film set expect = 0 where film_id = ?";
		return temp.update(sql,filmId);
	}



	@Override
	public List<String> selectFilmShowTime() {
		String sql = "select DISTINCT film_showTime from film where is_delete=0";
		return temp.query(sql, new FilmShowTimeRowMapper());
	}


}
